package com.naja.auth2server.config;

import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.naja.auth2server.entity.User;
import com.naja.auth2server.security.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.security.jackson2.SecurityJackson2Modules;

import java.util.List;

/**
 * @auther wangjianying
 * @date 2023/12/19 14:50
 */
@Configuration
public class JacksonConfig {

    @Bean("oauth2ObjectMapper")
    public ObjectMapper oauth2ObjectMapper(Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder) {
        List<Module> securityModules = SecurityJackson2Modules.getModules(this.getClass().getClassLoader());
        ObjectMapper objectMapper = jackson2ObjectMapperBuilder.build();
        objectMapper.registerModules(securityModules);
        objectMapper.addMixIn(LoginUser.class, LoginUserMixin.class);
        objectMapper.addMixIn(User.class, UserMixin.class);
        objectMapper.addMixIn(Long.class, LongMixin.class);
        objectMapper.addMixIn(Byte.class, ByteMixin.class);
        objectMapper.addMixIn(Integer.class, IntegerMixin.class);
        return objectMapper;
    }
    @Bean("objectMapper")
    @Primary
    public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.build();
        return objectMapper;
    }
}
